package com.gomro.galaxy.foundation.bi.configuration;

import org.springframework.batch.core.configuration.annotation.EnableBatchProcessing;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.jdbc.DataSourceProperties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.client.SpringCloudApplication;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Primary;

import javax.annotation.PostConstruct;
import javax.sql.DataSource;
import java.util.TimeZone;

@SpringCloudApplication
@ComponentScan(basePackages = {"com.gomro.galaxy.core.exception", "com.gomro.galaxy.foundation.bi"})
@EnableBatchProcessing
//@EnableSwagger2
public class BiApplication {

    @Primary
    @Bean("batchDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.batch")
    public DataSourceProperties batchDataSourceProperties() {

        return new DataSourceProperties();
    }

    @Primary
    @Bean("batchDataSource")
    public DataSource batchDataSource(@Qualifier("batchDataSourceProperties") DataSourceProperties dataSourceProperties) {

        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean("gomroDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.gomro")
    public DataSourceProperties gomroDataSourceProperties() {

        return new DataSourceProperties();
    }

    @Bean(name = "gomroDataSource")
    public DataSource gomroDataSource(@Qualifier("gomroDataSourceProperties") DataSourceProperties dataSourceProperties) {

        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @Bean("goodsDataSourceProperties")
    @ConfigurationProperties(prefix = "spring.datasource.goods")
    public DataSourceProperties goodsDataSourceProperties() {

        return new DataSourceProperties();
    }


    @Bean(name = "goodsDataSource")
    public DataSource goodsDataSource(@Qualifier("goodsDataSourceProperties") DataSourceProperties dataSourceProperties) {

        return dataSourceProperties.initializeDataSourceBuilder().build();
    }

    @PostConstruct
    void setDefaultTimezone() {

        TimeZone.setDefault(TimeZone.getTimeZone("Asia/Shanghai"));
    }

    public static void main(String[] args) {

        SpringApplication.run(BiApplication.class, args);
    }
}
